import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress' // 顶部进度条
// import 'nprogress/nprogress.css'// 已经在其他地方引入了自定义样式
import statistics from '@/utils/statistics' // 统计

import Routes from './routes'

NProgress.configure({ showSpinner: false }) // 配置进度条，去除进度条loading

statistics.init('4dc655ed71352ec8dc3a35a2e9d29283')

Vue.use(Router)

// 实例化路由
const router = new Router({
  base: '',
  // mode: 'history', // require service support (另外默认支持hash)
  scrollBehavior(to, from, savedPosition) {
    return savedPosition || { x: 0, y: 0 }
  },
  routes: Routes
})

// 路由声明周期
router.beforeEach(async (to, from, next) => {
  NProgress.start() // 顶部进度条开始
  // 登陆
  next()
})

router.afterEach(route => {
  // 设置路由title
  if (route.meta && route.meta.title) {
    document.title = route.meta.title
  }

  // 拿到正确的路径，多以添加延迟
  setTimeout(() => {
    statistics.trackPageView(route.fullPath) // 百度统计
  }, 300)

  NProgress.done()
})

export default router
